package com.yh.controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yh.bean.ChatPost;
import com.yh.bean.ChatReply;
import com.yh.bean.Game;
import com.yh.bean.GameClass;
import com.yh.bean.User;
import com.yh.service.ChatInf;

@Controller
public class ChatController {

	@Autowired
	private ChatInf chatimp;
	
	@RequestMapping("login")
	public String login(HttpServletRequest request){
		HttpSession session= request.getSession();
		User user=new User();
		user.setUid(3);
		user.setUname("社会李");
		session.setAttribute("user", user);
	    
		return "redirect:/html/ChatIndex.jsp";
	}
	
	@RequestMapping("showGameClass")
	@ResponseBody
	public List<GameClass> showGameClass(){//显示所有的游戏分类
		List<GameClass> gameClassList= chatimp.selectGameClass();
		//System.out.println("控制层的到的结果"+gameClassList);
		return gameClassList;
	}
	
	@RequestMapping("showGame")
	@ResponseBody
	public List<Game> showGame(int gcId){//显示所有的游戏
		List<Game> gameList= chatimp.selectGameBygcId(gcId);
		//System.out.println("根据分类Id获取到的游戏"+gameList);
		return gameList;
	}
	
	@RequestMapping("showAllChat")
	@ResponseBody
	public Map showAllChat(String postTitle,String currentPage){//展示所有的帖子或根据搜索展示帖子
		//System.out.println("前台接受到的帖子标题为"+postTitle);
		int curr=1;
		if(currentPage!=null){
			curr=Integer.parseInt(currentPage);
		}
		System.out.println("当前页数为："+currentPage);
		Map chatPostMap= chatimp.selectAllChat(postTitle,curr);
		//System.out.println("查到的所有帖子"+chatList);
		return chatPostMap;
	}
	
	
	@RequestMapping("showGameChat")
	public String showGameChat1(String gid,Integer currentPage,Model model,HttpServletRequest request){//通过点击游戏名称展示该游戏下的帖子
		int gameid=0;
		if(gid!=null && gid!=""){
			 gameid=Integer.parseInt(gid);
		}
		
		if(currentPage==null){
			currentPage=1;
		}
		  /*HttpSession session=request.getSession();
		 User user= (User) session.getAttribute("user");
		 if(user!=null){
			 int uid= user.getUid();
			 String uname=user.getUname();
		 }*/
		 
		Map<String,Object> map= chatimp.selectGameChat(gameid,currentPage);
		String gameName= chatimp.getGameName(gameid);
		List<ChatPost> gameChatList= (List) map.get("gameChatList");
		int pages=(int) map.get("pages");
		int curr= (int) map.get("curr");
		model.addAttribute("pages", pages);
		model.addAttribute("curr", curr);
		model.addAttribute("gameChatList", gameChatList);
		model.addAttribute("gameName", gameName);
		model.addAttribute("gameId", gameid);
	/*	model.addAttribute("uname", uname);*/
		
		//System.out.println("传输的游戏Id为"+gameid);
		return "showChatByGame";
	}
	
	@RequestMapping("showGameChatBySearch")
	public String showGameChatBySearch(String gameName,Model model){//通过搜索游戏名称展示该游戏下的帖子
		Game game= chatimp.searchGameByName(gameName);
        String result="";
		if(game!=null){
			List<ChatPost> gameChatList= chatimp.selectGameChatBySearch(gameName);
			model.addAttribute("gameChatList", gameChatList);
			System.out.println("查到的帖子信息为"+gameChatList);
			model.addAttribute("gameName", gameName);
			model.addAttribute("gameId", game.getGameId());
			result="showChatByGame";

		}else{
			result="NoResult";
		}
		return result; 
	}
	
	@RequestMapping("chatContent")
	public String chatContent(String postId,Integer currentPage,String postTitle,String postContent,String gameName,String gameId,String uname,String photo,String postPicture,Model model){ //通过点击展示该主贴及主贴的回复
		 
		/*System.out.println("帖子ID为："+postId);
		System.out.println("帖子标题为："+postTitle);*/
		System.out.println("跳转是传递的游戏Id为");
		int pid=0;
		if(postId!=null && postId!=""){
			pid=Integer.parseInt(postId);
		}
		
		if(currentPage==null){
			currentPage=1;
		}
		Map map= chatimp.selectChatReply(pid,currentPage);
		List<ChatReply> chatReplyList=(List<ChatReply>) map.get("chatReplyList");
		int curr=(int) map.get("curr");
		int pages=(int) map.get("pages");
		model.addAttribute("postTitle", postTitle);
		model.addAttribute("chatReplyList", chatReplyList);		
		model.addAttribute("postContent", postContent);
		model.addAttribute("gameName", gameName);
		model.addAttribute("gameId", gameId);
		model.addAttribute("uname", uname);
		model.addAttribute("pages", pages);
		model.addAttribute("curr", curr);
		model.addAttribute("postId", pid);//传递主贴Id用于新增是回复时返回该页面时找到该主贴
		model.addAttribute("postPicture", postPicture);
		if(photo!=null && photo!=""){
			model.addAttribute("photo", photo);
		}
		
		return "showChatContent";
	}
	
	@RequestMapping("addChatPost.do")
	public String addChatPost(Integer gameid,Integer currentPage,Integer uid,String gameName,String title,String postContent,HttpServletRequest request,@RequestParam(value="image")MultipartFile mf,Model model){
		ChatPost cp=new ChatPost();
		Game game=new Game();
		game.setGameId(gameid);
		User user=new User();
		user.setUid(uid);
		cp.setPostTitle(title);
		cp.setPostContent(postContent);
		cp.setUser(user);
		cp.setGame(game);
		
		Date date =new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String postTime= sdf.format(date);
		
		cp.setPostTime(postTime);
		cp.setPostStatus(0);
        //文件上传
		String fileName= mf.getOriginalFilename();
		cp.setPostPicture("images/"+fileName);
		
		if(fileName!=null && fileName!=""){		
		 String realPath=request.getServletContext().getRealPath("images");
		 String uploadPath=realPath+"/"+fileName;
		 try {
			IOUtils.copy(mf.getInputStream(), new FileOutputStream(uploadPath));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		chatimp.addChatPost(cp);
		if(currentPage==null){
			currentPage=1;
		}
		Map map= chatimp.selectGameChat(gameid,currentPage);
		List<ChatPost> gameChatList= (List<ChatPost>) map.get("gameChatList");
		int pages= (int) map.get("pages");
		int curr= (int) map.get("curr");
		model.addAttribute("pages", pages);
		model.addAttribute("curr", curr);
		model.addAttribute("gameid", gameid);
		model.addAttribute("gameChatList", gameChatList);
		System.out.println("查到的帖子信息为"+gameChatList);
		model.addAttribute("gameName", gameName);
		return "showChatByGame";
	}
	
	@RequestMapping("addPostReply.do")
	public String addPostReply(Integer uid,Integer currentPage,String gameName,String postTitle,String postContent,String uname,Integer gameId,Integer postId,String replyTontent,HttpServletRequest request,@RequestParam(value="image")MultipartFile mf,Model model){
		
		if(currentPage==null){
			currentPage=1;
		}		
		User user=new User();
		ChatReply cr=new ChatReply();             
		user.setUid(uid);                                         
		cr.setReplyTontent(replyTontent);
		
		String fileName= mf.getOriginalFilename();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String postTime= sdf.format(new Date());
		cr.setReplyTime(postTime);
		cr.setUser(user);
		System.out.println(""+cr);
		cr.setPost_id(postId);		
		if(fileName!=null && fileName!=""){//判断文件名是否为空来判断是否添加了图片
			cr.setReplyPicture("images/"+fileName);
			 String realPath=request.getServletContext().getRealPath("images");
			 String uploadPath=realPath+"/"+fileName;
			 try {
				IOUtils.copy(mf.getInputStream(), new FileOutputStream(uploadPath));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(cr);
		chatimp.addChatReply(cr);		
		model.addAttribute("gameName", gameName);
		model.addAttribute("gameId", gameId);
		model.addAttribute("postTitle", postTitle);
		Map map= chatimp.selectChatReply(postId,currentPage);
		List<ChatReply> chatReplyList=(List<ChatReply>) map.get("chatReplyList");
		int curr= (int) map.get("curr");
		int pages=(int) map.get("pages");
		model.addAttribute("curr", curr);
		model.addAttribute("pages", pages);
		model.addAttribute("chatReplyList", chatReplyList);		
		model.addAttribute("postContent", postContent);
		model.addAttribute("postId", postId);
		model.addAttribute("uname", uname);
		return "showChatContent";
	}
	
	@RequestMapping("getAllGame")
	@ResponseBody
	public List<Game> getAllGame(){
		List<Game> gameList= chatimp.selectAllGame();
		System.out.println("查询到的所有的游戏："+gameList);
		return gameList;
	}
	
	@RequestMapping("getAdminAllChat.se")
	@ResponseBody
	public Map<String,Object> getAdminAllChat(Integer gameId,Integer currentPage){  //管理员首页面展示
		int id=0;
		if(gameId!=null){
			id=gameId;
		}
		if(currentPage==null){
			currentPage=1;
		}
		System.out.println("游戏Id为："+gameId+"     当前也为："+currentPage);
		Map<String,Integer> map1=new HashMap<String, Integer>();
		map1.put("gameId", id);
			System.out.println("前段传来的当前页"+currentPage);
		Map<String,Object> map= chatimp.adminSelectChat(map1,currentPage);
		return map;
	}
	
	@RequestMapping("adminUpdateChat.se")
	public void adminUpdateChat(int postId){    //管理员改变帖子的状态(封禁帖子)
		chatimp.amdinUpdateChatPost(postId);
	}
	
	@RequestMapping("adminDeleteChat.se")
	public void adminDeleteChat(int postId){     //管理员删除帖子
		chatimp.adminDeleteChatPost(postId);
	}
	
	@RequestMapping("adminUpdateMoreChat.se")
	public void adminUpdateMoreChat(String gameIdList){ //管理员批量封禁用户
		String[] giList= gameIdList.split(" ");
		for(int i=0;i<giList.length;i++){
			int postId=Integer.parseInt(giList[i]);
			chatimp.amdinUpdateChatPost(postId);
		}
		
	}
	
	@RequestMapping("adminDeleteMoreChat.se")
	public void adminDeleteMoreChat(String postIdList){//管理员批量删除帖子
		String[] pIdList=postIdList.split(" ");
		for(int i=0;i<pIdList.length;i++){
			int postId=Integer.parseInt(pIdList[i]);
			//System.out.println(postId);
			chatimp.adminDeleteChatPost(postId);
		}
	}
	
	@RequestMapping("adminSearchChat.se")
	@ResponseBody
	public Map<String,Object> adminSearchChat(String gameName,String postTitle,String postTime,int currentPage){ //管理员搜索帖子（正常状态下）
		
		System.out.println("控制层测试传来的值："+gameName+postTitle+postTime);
		Map<String,String> map=new HashMap<String, String>();
		map.put("gameName", gameName);
		map.put("postTitle", postTitle);
		map.put("postTime", postTime);
		Map<String,Object> map1= chatimp.adminSearchChat(map,currentPage);
		System.out.println("控制层测试查询到的数据："+map1.get("cpList"));
		return map1;
	}
	
	@RequestMapping("AdminAllStopChat.se")
	@ResponseBody
	public Map<String,Object> AdminAllStopChat(Integer gameId,Integer currentPage){  //管理员首页面展示及通过游戏展示
		int id=0;
		if(gameId!=null){
			id=gameId;
		}
		if(currentPage==null){
			currentPage=1;
		}
		System.out.println("游戏Id为："+gameId+"     当前也为："+currentPage);
		Map<String,Integer> map1=new HashMap<String, Integer>();
		map1.put("gameId", id);
			System.out.println("前段传来的当前页"+currentPage);
		Map<String,Object> map= chatimp.adminSelectStopChat(map1,currentPage);
		return map;
	}
	
	@RequestMapping("adminSearchStopChat.se")
	@ResponseBody
	public Map<String,Object> adminSearchStopChat(String gameName,String postTitle,String postTime,int currentPage){ //管理员搜索帖子（正常状态下）
		
		System.out.println("控制层测试传来的值："+gameName+postTitle+postTime);
		Map<String,String> map=new HashMap<String, String>();
		map.put("gameName", gameName);
		map.put("postTitle", postTitle);
		map.put("postTime", postTime);
		Map<String,Object> map1= chatimp.adminSearchStopChat(map,currentPage);
		System.out.println("控制层测试查询到的数据："+map1.get("cpList"));
		return map1;
	}
	
	@RequestMapping("adminUpdateStopChat.se")
	public void adminUpdateStopChat(int postId){    //管理员改变帖子的状态(解封帖子)
		chatimp.adminUpdateStopChat(postId);
	}
		
	
	@RequestMapping("adminUpdateMoreStopChat.se")
	public void adminUpdateMoreStopChat(String gameIdList){ //管理员批量解封用户
		String[] giList= gameIdList.split(" ");
		for(int i=0;i<giList.length;i++){
			int postId=Integer.parseInt(giList[i]);
			chatimp.adminUpdateStopChat(postId);
		}		
	}
	
	@RequestMapping("userAddLoveGame.do")
	public void userAddLoveGame(int gameId,HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");	
		int uid=user.getUid();
		chatimp.userAddGame(gameId, uid);
	}
			
		
	@RequestMapping("testLoveOrNot.do")
	@ResponseBody
	public boolean testLoveOrNot(Integer gameId,HttpServletRequest request){ //讨论区判断是否已经关注该游戏
		boolean result=true;
		 User user=(User) request.getSession().getAttribute("user");		 
			 int uid=user.getUid();		 
		if(gameId!=null && uid!=0){
			result= chatimp.testLoveOrNo(gameId, uid);
		}		
		return result;
	}
	
	@RequestMapping("userDeleteLoveGame.do")
	public void userDeleteLoveGame(int gameId,HttpServletRequest request){//取消关注
		User user=(User) request.getSession().getAttribute("user");	
		int uid=user.getUid();
		chatimp.userDeleteLoveGame(gameId, uid);
	}
	
	@RequestMapping("getLoveGame.do")
	@ResponseBody
	public List<Game> getLoveGame(HttpServletRequest request){  //展示用户关注的游戏
		System.out.println("测试测试测试测试测试测试");
		User user=(User) request.getSession().getAttribute("user");	
		int uid=user.getUid();
		System.out.println("uid++++++"+uid);
		List<Game> loveGameList= chatimp.getLoveGame(uid);
		System.out.println("lovaGameList+++++"+loveGameList);
		return loveGameList;
	}
	
	@RequestMapping("getUser.do")
	@ResponseBody
	public User getUser(HttpServletRequest request){  //获取用户信息
		User user1=(User) request.getSession().getAttribute("user");	
		int uid=user1.getUid();
		User user= chatimp.getUser(uid);
		System.out.println("用户图像"+user.getPhoto());
		System.out.println("用户姓名"+user.getUname());
		return user;
	}
	
	@RequestMapping("notLoveGame.do")
	@ResponseBody
	public  void notLoveGame(int gameId,HttpServletRequest request){//取消关注
		User user=(User) request.getSession().getAttribute("user");	
		int uid=user.getUid();
		chatimp.userDeleteLoveGame(gameId, uid);
	}
	
	@RequestMapping("getUserPost.do")
	@ResponseBody
	public Map<String,Object> getUserPost(int currentPage,HttpServletRequest request){ //展示用户自己的发帖
		User user=(User) request.getSession().getAttribute("user");	
		int uid=user.getUid();
		Map<String,Object> map= chatimp.getUserPost(uid,currentPage);
		return map;
	}
	
	@RequestMapping("getUserReply.do")
	@ResponseBody
	public Map<String,Object> getUserReply(int currentPage,HttpServletRequest request){  //展示用户所有的回复
		User user=(User) request.getSession().getAttribute("user");	
		int uid=user.getUid();
		Map<String,Object> map= chatimp.getUserReply(uid, currentPage);
		
		return map;
	}
	
	
}
